<?php
namespace app\house\controller;
use app\AdminRest;
use app\dynamic\model\CardUser;
use app\dynamic\model\UserInfo;
use app\house\model\AdminHoseStaff;
use app\house\model\LongPage;
use app\house\model\LongPageRecord;
use think\App;
use think\Request;
use app\house\model\AdminHouse as house_model;
use app\house\model\AdminApartment;
use app\house\model\AdminArea;
use app\house\model\AdminCate;
class AdminHouse extends AdminRest
{

    protected $model;
    protected $partment_model;
    protected $area_model;
    protected $cate_model;
    public function __construct(App $app) {
        parent::__construct($app);
        $this->model          = new house_model();
        $this->partment_model = new AdminApartment();
        $this->area_model     = new AdminArea();
        $this->cate_model     = new AdminCate();
    }

    /**
     * 房源列表
     */
    public function houseList(){

        $input = $this->_param;

        if(!empty($input['keyword'])){
            $dis[] = ['title','like',"%".$input['keyword']."%"];
        }

        if(!empty($input['status'])){
            $dis[] = ['status','=',$input['status']];
        }
        //分类
        if(!empty($input['cate'])){

            $dis[] = ['category','=',$input['cate']];

        }
        //类型
        if(isset($input['house_type'])&&$input['house_type']!=999){

            $dis[] = ['house_type','=',$input['house_type']];

        }
        $dis[] = ['uniacid','=',$this->_uniacid];

        $data  = $this->model->houseList($dis,$input['limit']);

        return $this->success($data);
    }

    /**
     *
     * 转换插入数据
     */
    public function changeData($data){
        $data['uniacid']         = $this->_uniacid;

        $data['attrs']           = json_encode($data['house'],JSON_UNESCAPED_UNICODE);

        $data['community_attrs'] = json_encode($data['area'],JSON_UNESCAPED_UNICODE);

        $data['imgs']            = !empty($data['imgs'])?implode(';',$data['imgs']):0;

        $data['img_info']        = !empty($data['img_info'])?implode(';',$data['img_info']):0;

        $data['labels']          = !empty($data['labels'])?implode(',',$data['labels']):'';

        $data['huxing_id']       = !empty($data['house']['户型'])?$data['house']['户型']:0;

        $data['price']           = !empty($data['house']['售价'])?$data['house']['售价']:0;

        $data['area_id']         = array_pop($data['area_id']);
        //新的户型
        $data['new_hx']          = !empty($data['new_hx'])?json_encode($data['new_hx'],JSON_UNESCAPED_UNICODE):'';

        $data['monomer_path']    = !empty($data['monomer_path'])?json_encode($data['monomer_path'],JSON_UNESCAPED_UNICODE):'';
        //效果图
        $data['design_img']      = !empty($data['design_img'])?implode(',',$data['design_img']):0;
        //实拍图
        $data['real_img']        = !empty($data['real_img'])?implode(',',$data['real_img']):0;
        //分类模型
        $house_cate = new AdminCate();
        //类型
        $type = $house_cate->where(['id'=>$data['category']])->value('type');

        $data['house_type'] = $type;
        //楼盘信息
        if(!empty($data['build'])){

            $data['build'] = json_encode($data['build'],JSON_UNESCAPED_UNICODE);
        }

        unset($data['house']);
        unset($data['area']);
        return $data;
    }

    /**
     *
     * 添加房源
     */
    public function houseAdd(){
        $input = $this->_input;
        $input = $this->changeData($input);
        $res   = $this->model->houseAdd($input);
        return $this->success($res);
    }

    /**
     *
     * 编辑房源
     */
    public function houseUpdate(){
        $input = $this->_input;
        if(count($input)>1){
            $input = $this->changeData($input);
            $data  = $this->model->houseUpdate(['id'=>$input['id']],$input);
        }else{
            $data = $this->model->houseInfo(['id'=>$input['id']]);

            $data['area_id'] = $this->areaId($data['area_id']);
        }

        return $this->success($data);
    }
    /**
     * User: chenniang
     * Date: 2019-09-18 09:42
     * @param $id
     * @return void
     * descption:获取房产区域id&array
     */
    public function areaId($id,$l=0){
        static $data = array();
        $l++;
        array_unshift($data,$id);
        $pid = $this->area_model->areaPid(['id'=>$id]);
        if(!empty($pid)&&$l<4){
            $this->areaId($pid,$l);
        }
        return $data;
    }
    /**
     *
     * 获取房源相关信息
     */
    public function houseAbout(){

        $input = $this->_input;

        $dis['uniacid']   = $this->_uniacid;
        $data['apartment']= $this->partment_model->apartmentSelect($dis);
        $dis['status']    = 1;
        $data['area']     = $this->area_model->areaListV2($dis);

//        if($input['type']!=-1){

//            $dis['type']  = $input['type'];
//        }

        $data['cate']     = $this->cate_model->cateSelect($dis);

        return $this->success($data);
    }
    /**
     * User: chenniang
     * Date: 2019-09-16 17:15
     * @return \think\Response
     * descption:上下架|删除
     */
    public function statusUpdate(){
        $input = $this->_input;
        $data  = $this->model->houseUpdate(['id'=>$input['id']],$input);
        return $this->success($data);
    }


    /**
     * @author chenniang
     * @DataTime: 2020-02-19 14:27
     * @功能说明:房源设置
     */
    public function houseSetting(){


        $data  = lbPulgSettingInfo($this->_uniacid,'card',['house_style']);

        return $this->success($data);

    }


    /**
     * @author chenniang
     * @DataTime: 2020-02-19 14:34
     * @功能说明:房源设置编辑
     */
    public function houseSettingUpdate(){

        $input = $this->_input;

        $res  = lbPulgSettingUpdate($this->_uniacid,'card','house_style',$input['status']);

        return $this->success($res);

    }



    /**
     * User: chenniang
     * Date: 2019-11-29 17:28
     * @return void
     * descption:获取员工
     */
    public function getStaffInfo(){
        $input = $this->_input;
        $user_model = new UserInfo();
        $user_info  = new CardUser();
//        $data = [];
        $dis[] = ['is_staff','=',1];
        $dis[] = ['uniacid','=',$this->_uniacid];
        if(!empty($input['name'])){
            $dis[]  = ['name','like',"%".$input['name']."%"];
        }
        $data = $user_model->where($dis)->field('fans_id as id,name')->select();
        if(!empty($data)){
            foreach ($data as &$v){
                if(empty($v['name'])){
                    $v['name'] = $user_info->where(['id'=>$v['id']])->value('nickName');
                }
            }
        }
        return $this->success($data);
    }


    /**
     * @author chenniang
     * @DataTime: 2021-12-12 14:19
     * @功能说明:长页面列表
     */
    public function longPageList(){

        $input = $this->_param;

        $dis[] = ['uniacid','=',$this->_uniacid];

        $dis[] = ['status','>',-1];

        if(!empty($input['title'])){

            $dis[] = ['title','like','%'.$input['title'].'%'];
        }

        $long_model = new LongPage();

        $data = $long_model->dataList($dis, $input['limit']);

        return $this->success($data);


    }


    /**
     * @author chenniang
     * @DataTime: 2021-12-12 14:21
     * @功能说明:长页面添加
     */
    public function longPageAdd(){

        $input = $this->_input;

        $input['uniacid'] = $this->_uniacid;

        $long_model = new LongPage();

        $res = $long_model->dataAdd($input);

        return $this->success($res);

    }


    /**
     * @author chenniang
     * @DataTime: 2021-12-12 14:22
     * @功能说明:长页面编辑
     */
    public function longPageUpdate(){

        $input = $this->_input;

        $dis = [

            'id' => $input['id']
        ];

        $input['uniacid'] = $this->_uniacid;

        $long_model = new LongPage();

        $res = $long_model->dataUpdate($dis,$input);

        return $this->success($res);

    }


    /**
     * @author chenniang
     * @DataTime: 2021-12-12 14:23
     * @功能说明:长页面详情
     */
    public function longPageInfo(){

        $input = $this->_param;

        $dis = [

            'id' => $input['id']
        ];

        $long_model = new LongPage();

        $res = $long_model->dataInfo($dis);

        return $this->success($res);

    }


    /**
     * @author chenniang
     * @DataTime: 2021-12-13 14:35
     * @功能说明:
     */
    public function longPageRecord(){

        $input = $this->_param;

        $dis = [

            'a.house_id' => $input['house_id']
        ];

        $long_model = new LongPageRecord();

        $res = $long_model->dataList($dis,$input['limit']);

        return $this->success($res);

    }




}
